<template>
  <div>
    <a-card :bodyStyle="tstyle" :bordered="false">
      <div v-if="hasPerm('property:page')" class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="名称：">
                <a-input v-model="queryParam.jobDesc" allow-clear placeholder="请输入名称" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :alert="true"
        :columns="columns"
        :data="loadData"
        :rowKey="(record) => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <span slot="scheduler" slot-scope="text, record">
          <a @click="$refs.log_detail.show(record,'查看调度器',0)">查看调度器</a>
        </span>
        <span slot="executiveSummary" slot-scope="text, record">
          <a @click="$refs.log_detail.show(record,'查看执行摘要',1)">查看执行摘要</a>
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('property:edit')" @click="$refs.log_detail.show(record,'查看日志',2)">查看日志</a>
        </span>
      </s-table>
    </a-card>
    <log-detail-dialog ref="log_detail"></log-detail-dialog>
  </div>
</template>
<script>

import { STable } from '@/components'
import { logMonitoringList } from '@/api/modular/main/knowl/log_monitoring/logMonitoringManager'
import logDetailDialog from './logDetailDialog'

export default {
  components: {
    STable,
    logDetailDialog
  },
  props: {
    queryParam: {
      type: Object,
      default: function() {
        return {}
      }
    },
    modal: {
      type: Boolean,
      default: function() {
        return false
      }
    }
  },
  data() {
    return {
      // 表头
      columns: [
        {
          title: '任务ID',
          align: 'center',
          dataIndex: 'jobId',
          ellipsis: true
        },
        {
          title: '任务名称',
          align: 'center',
          dataIndex: 'jobDesc',
          ellipsis: true
        },
        {
          title: '调度时间',
          align: 'center',
          dataIndex: 'triggerTime',
          width: '180px',
          customRender: (t, r, index) => {
            if (r.triggerTime) {
              return this.$moment(r.triggerTime).format('YYYY-MM-DD HH:mm:ss')
            }
            return ''
          }
        },
        {
          title: '调度结果',
          align: 'center',
          ellipsis: true,
          customRender: function(t, r, index) {
            if (r.triggerCode === 200) {
              return '成功'
            }
            return '失败'
          }
        },
        {
          title: '调度器信息',
          align: 'center',
          scopedSlots: { customRender: 'scheduler' }
        },
        {
          title: '执行时间',
          align: 'center',
          dataIndex: 'handleTime',
          width: '180px',
          customRender: (t, r, index) => {
            if (r.handleTime) {
              return this.$moment(r.handleTime).format('YYYY-MM-DD HH:mm:ss')
            }
            return ''
          }
        },
        {
          title: '执行结果',
          align: 'center',
          customRender: function(t, r, index) {
            if (r.handleCode === 200) {
              return '成功'
            }
            return '失败'
          }
        },
        {
          title: '执行结果摘要',
          align: 'center',
          scopedSlots: { customRender: 'executiveSummary' }
        }
      ],
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return logMonitoringList(Object.assign(parameter, this.queryParam)).then((res) => {
          const content = res.content
          const data = content.data
          return {
            'pageNo': parameter.pageNo,
            'pageSize': data.length,
            'totalPage': parameter.pageSize,
            'totalRows': content.recordsTotal,
            'rows': data
          }
        })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  created() {
    if (this.hasPerm('property:edit') || this.hasPerm('property:delete')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
  },
  methods: {
    tableReload() {
      this.handleOk()
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }
}
</script>
<style lang="less" scoped>
.table-operator {
  margin-bottom: 18px;
}

button {
  margin-right: 8px;
}
</style>
